package cn.tianyigps.tianyigpsapiv2.controller;

import cn.tianyigps.tianyigpsapiv2.oauth2.context.ClientPrincipal;
import cn.tianyigps.tianyigpsapiv2.param.vo.ResponseVo;
import cn.tianyigps.tianyigpskafka.task.bean.Task;
import cn.tianyigps.tianyigpskafka.task.manger.TaskManger;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IDEA
 * author:guibin.cui
 * Date:2019/2/16
 * Time:18:02
 */

@RestController
@RequestMapping("/task")
@CrossOrigin
@Api(value = "TaskController", description = "任务类服务接口")
public class TaskController {

    @Autowired
    private TaskManger taskManger;

    @PostMapping
    @ApiOperation(value = "添加任务", notes = "任务类服务接口 {\n" +
            "  \"nextTask\": {\n" +
            "  \"nextTask\": null,\n" +
            "  \"params\": {\"callback\":\"http://www.baidu.com\"},\n" +
            "  \"taskName\": \"callback001\",\n" +
            "  \"taskType\": \"callback001\",\n" +
            "},\n" +
            "  \"params\": {\"equipmentIds\":[\"434823467483\",\"5345345345\"]},\n" +
            "  \"taskName\": \"tasktest001\",\n" +
            "  \"taskType\": \"tasktest\",\n" +
            "}")
    public ResponseVo addTask(@RequestBody Task task) {

        if (StringUtils.isEmpty(task.getTaskType())) {
            return ResponseVo.badRequest("任务类型不能为空！");
        }
//        boolean http = callBackUrl.startsWith("http");
//        if (!http){
//            return ResponseVo.badRequest("回调地址前需加 'http://' 或 'https://' ");
//        }
        task.setClientId(ClientPrincipal.getClientId());
        taskManger.distributeTaskAndSave(task);
        return ResponseVo.success("任务下发成功。", task);
    }
}
